package com.train.implementCallable;

import java.util.concurrent.Callable;
import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;

/**
 * Callable接口有个好处就是，执行多线程方法有返回值，通过{@link Future#get()}获取返回值。
 * 实现{@link Callable}接口的{@link Callable#call()}方法，
 * 用{@link Executors#newFixedThreadPool(int)}方法初始化一个线程池，
 * 并将此类的实例化对象提交给线程池({@link ExecutorService#submit(Callable)})执行
 * 调用{@link ExecutorService#shutdown()}停止线程池的运行。
 * */
public class ImplementCallable implements Callable<Boolean>{
    public static Integer count = 0;
    public ImplementCallable(){}
    @Override
    public Boolean call() throws Exception {
        String threadName = Thread.currentThread().getName();
        Integer sleepTime;
        switch (threadName){
            case "pool-1-thread-3":{sleepTime = 150;};
            case "pool-1-thread-2":{sleepTime = 200;};
            default:{sleepTime = 180;};
        }
        for (int i = 0; i < 10; i++) {
            Thread.sleep(sleepTime);
            System.out.println(threadName+" count:"+count++);
        }
        return Boolean.TRUE;
    }
}
